﻿using System;
using System.Data;

using BLL;
using Model;
using Utility;

namespace EvaluationSystem
{
    public partial class AddQuestion : System.Web.UI.Page
    {
        private string m_question_id = string.Empty ;
        private QuestionBLL m_questionBll = new QuestionBLL();
        private const string ADD_QUESTION_ERROR = "题目保存失败，可能是由于:题目编号重复，请重新添加修改题目信息.";
        
        protected void Page_Load(object sender, EventArgs e)
        {
            ((SiteSystemMaster)Master).InitMenu();
            ((SiteSystemMaster)Master).li3.Attributes.Add("class", "hover ccc");
            ((SiteSystemMaster)Master).b_hmenu1.Style.Add("display", "none");
            ((SiteSystemMaster)Master).b_hmenu2.Style.Add("display", "none");
            ((SiteSystemMaster)Master).b_hmenu3.Style.Clear();
            ((SiteSystemMaster)Master).s_menu3_2.Style.Add("COLOR", "#147ab8");
            ((SiteSystemMaster)Master).s_menu3_2.Style.Add("TEXT-DECORATION", "none"); 

            /*判断是否是管理员身份*/
            if (Session["username"] == null)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), 
                    "PopupScript", "<script>alert('请先登录系统!');top.location.href='../System/Login.aspx';</script>");
                return;
            }
            else
            {
                if (!IsPostBack)
                {
                    if (Request.QueryString["question_id"] != null)
                    {
                        m_question_id = Request.QueryString["question_id"];
                        DataSet ds = m_questionBll.GetQuestionById(m_question_id);
                        ddlDeminsion.SelectedValue = ds.Tables[0].Rows[0][13].ToString();
                        txtbQuestionNo.Text = ds.Tables[0].Rows[0][1].ToString();
                        txtbQuestionNo.ReadOnly = true;//编辑现有题目时禁止修改题号，只允许修改内容。
                        
                        fckEtitle.Value = ds.Tables[0].Rows[0][2].ToString();
                        fckEA.Value = ds.Tables[0].Rows[0][3].ToString();
                        fckEB.Value = ds.Tables[0].Rows[0][4].ToString();
                        fckEC.Value = ds.Tables[0].Rows[0][5].ToString();
                        fckED.Value = ds.Tables[0].Rows[0][6].ToString();
                        fckEE.Value = ds.Tables[0].Rows[0][7].ToString();
                        
                        txtbAscore.Text = ds.Tables[0].Rows[0][8].ToString();
                        txtbBscore.Text = ds.Tables[0].Rows[0][9].ToString();
                        txtbCscore.Text = ds.Tables[0].Rows[0][10].ToString();
                        txtbDscore.Text = ds.Tables[0].Rows[0][11].ToString();
                        txtbEscore.Text = ds.Tables[0].Rows[0][12].ToString();
                    }
                    if (Request.QueryString["IsReadOnly"] != null)
                    {
                        if (Int32.Parse(Request.QueryString["IsReadOnly"]) == 1)
                        {
                            txtbAscore.ReadOnly = true;
                            txtbBscore.ReadOnly = true;
                            txtbCscore.ReadOnly = true;
                            txtbDscore.ReadOnly = true;
                            txtbEscore.ReadOnly = true;
                            txtbQuestionNo.ReadOnly = true;
                            btnSave.Visible = false;
                        }
                    }
                    bind();
                }
            }
        }

        private void bind()
        {
            DimensionBLL dimensionBll = new  DimensionBLL();
            DataSet dsPosition = dimensionBll.GetDimensionAll();
            ddlDeminsion.DataSource = dsPosition.Tables[0].DefaultView;

            ddlDeminsion.DataTextField = "dimension_name";
            ddlDeminsion.DataValueField = "dimension_id";
            ddlDeminsion.DataBind();

            if (Request.QueryString["dimID"] != null)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["dimID"]))
                {
                    ddlDeminsion.SelectedValue = Request.QueryString["dimID"];
                }
            }
        }

        protected void btnSave_Click1(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtbQuestionNo.Text))
            {
                MessageBox.ShowAndRedirect(this, "请添加题号信息", 
                    "QuestionList.aspx?dimID=" + ddlDeminsion.SelectedValue);
                return;
            }

            QuestionBLL questionBll = new QuestionBLL();
            Question temp = new Question();

            temp.Question_no = int.Parse(txtbQuestionNo.Text.Trim());
            temp.Question_typeid = 1;//单选题
            temp.Question_status = 1;//0:禁用状态 1:启用状态
            temp.Question_title = fckEtitle.Value.Replace("<p>", "").Replace("</p>", "");
            temp.Question_dimensionid = int.Parse(ddlDeminsion.SelectedValue);
            temp.Question_answer_A = fckEA.Value.Replace("<p>", "").Replace("</p>", "");
            temp.Question_answer_A_score = txtbAscore.Text.Trim().Equals(string.Empty) ? 0 : int.Parse(txtbAscore.Text.Trim());
            temp.Question_answer_B = fckEB.Value.Replace("<p>", "").Replace("</p>", "");
            temp.Question_answer_B_score = txtbBscore.Text.Trim().Equals(string.Empty) ? 0 : int.Parse(txtbBscore.Text.Trim());
            temp.Question_answer_C = fckEC.Value.Replace("<p>", "").Replace("</p>", "");
            temp.Question_answer_C_score = txtbCscore.Text.Trim().Equals(string.Empty) ? 0 : int.Parse(txtbCscore.Text.Trim());
            temp.Question_answer_D = fckED.Value.Replace("<p>", "").Replace("</p>", "");
            temp.Question_answer_D_score = txtbDscore.Text.Trim().Equals(string.Empty) ? 0 : int.Parse(txtbDscore.Text.Trim());
            temp.Question_answer_E = fckEE.Value.Replace("<p>", "").Replace("</p>", "");
            temp.Question_answer_E_score = txtbEscore.Text.Trim().Equals(string.Empty) ? 0 : int.Parse(txtbEscore.Text.Trim());

            if (Request.QueryString["question_id"] == null)
            {
                int nRows = questionBll.Add(temp);
                if (nRows > 0)
                {
                    MessageBox.ShowAndRedirect(this, "条目添加成功", 
                        "QuestionList.aspx?dimID=" + ddlDeminsion.SelectedValue);
                }
                else
                {
                    MessageBox.ShowAndRedirect(this, ADD_QUESTION_ERROR, 
                        "QuestionList.aspx?dimID=" + ddlDeminsion.SelectedValue);
                    return;
                }
            }
            else
            {
                temp.Question_id = int.Parse(Request.QueryString["question_id"].ToString());
                int resultTemp = questionBll.Update(temp);
                if (resultTemp > 0)
                {
                    MessageBox.ShowAndRedirect(this, "条目修改成功", 
                        "QuestionList.aspx?dimID=" + ddlDeminsion.SelectedValue);
                }
                else
                {
                    MessageBox.ShowAndRedirect(this, ADD_QUESTION_ERROR, 
                        "QuestionList.aspx?dimID=" + ddlDeminsion.SelectedValue);
                    return;
                }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("QuestionList.aspx?dimID=" + ddlDeminsion.SelectedValue);
        }

        protected void btnPrev_Click(object sender, EventArgs e)
        {
            MessageBox.ShowAndRedirect(this, "请在条目列表中选择题目进行修改。", "QuestionList.aspx");
        }
    }
}